package com.example.springdemo.controller;

import com.example.springdemo.dao.IngredientRepository;
import com.example.springdemo.entity.Ingredient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

@RestController
@RequestMapping(path="/ingredientsx", produces="application/json")
@CrossOrigin(origins="*")
public class IngredientController {
    @Resource
    private IngredientRepository repo;

    @GetMapping
    public Iterable<Ingredient> allIngredients() {
        return repo.findAll();
    }

    @GetMapping("/{id}")
    public Ingredient getIngredientById(@PathVariable("id") String id){
        Optional<Ingredient> optionalIngredient = repo.findById(id);
        return optionalIngredient.orElseGet(Ingredient::new);
    }

}